package cmj.cloudmusic.fragment.Discover;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cmj.cloudmusic.R;
import cmj.cloudmusic.activity.MainActivity;
import cmj.cloudmusic.entity.MusicRank;
import cmj.cloudmusic.fragment.RankItem_fragment;

/**
 * Created by 王虎 on 2016/12/11.
 */
public class D_bangFragment extends Fragment {

    Context context;
    ListView listView;
    List<MusicRank> list;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.d_paihang_fragment,null);
        listView= (ListView) view.findViewById(R.id.listview);
        list=new ArrayList<>();

        list.add(new MusicRank(R.drawable.c1,"","每周四更新","1.南屏晚钟-华晨宇/苏诗丁","2.南屏晚钟-华晨宇/苏诗丁","3.南屏晚钟-华晨宇/苏诗丁"));
        list.add(new MusicRank(R.drawable.c2,"","每周四更新","1.南屏晚钟-华晨宇/苏诗丁","2.南屏晚钟-华晨宇/苏诗丁","3.南屏晚钟-华晨宇/苏诗丁"));
        list.add(new MusicRank(R.drawable.c3,"","每周四更新","1.南屏晚钟-华晨宇/苏诗丁","2.南屏晚钟-华晨宇/苏诗丁","3.南屏晚钟-华晨宇/苏诗丁"));
        list.add(new MusicRank(R.drawable.c4,"","每周四更新","1.南屏晚钟-华晨宇/苏诗丁","2.南屏晚钟-华晨宇/苏诗丁","3.南屏晚钟-华晨宇/苏诗丁"));
        list.add(new MusicRank(R.mipmap.mouse,"老鼠们最喜欢的歌","每天更新","","",""));
        list.add(new MusicRank(R.mipmap.mouse,"老鼠们最喜欢的歌","每天更新","","",""));
        list.add(new MusicRank(R.mipmap.mouse,"老鼠们最喜欢的歌","每天更新","","",""));
        list.add(new MusicRank(R.mipmap.mouse,"老鼠们最喜欢的歌","每天更新","","",""));
        list.add(new MusicRank(R.mipmap.mouse,"老鼠们最喜欢的歌","每天更新","","",""));
        list.add(new MusicRank(R.mipmap.mouse,"老鼠们最喜欢的歌","每天更新","","",""));
        list.add(new MusicRank(R.mipmap.mouse,"老鼠们最喜欢的歌","每天更新","","",""));
        list.add(new MusicRank(R.mipmap.mouse,"老鼠们最喜欢的歌","每天更新","","",""));
        list.add(new MusicRank(R.mipmap.mouse,"老鼠们最喜欢的歌","每天更新","","",""));

        DemoAdapter adapter=new DemoAdapter();

        listView.setAdapter(adapter);

        return view;
    }
    class DemoAdapter extends BaseAdapter{
        @Override
        public int getItemViewType(int position) {
            if (position<4){
                return 0;
            }else return 1;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (getItemViewType(position)==0){
               return getView1(position,convertView,parent);
            }else {
               return getView2(position,convertView,parent);
            }
        }
        public View getView1(final int position, View convertView, ViewGroup parent) {
           ViewHolder1 viewHolder1=null;
            if (convertView==null){
                convertView=LayoutInflater.from(context).inflate(R.layout.paihang_item1,null);
                viewHolder1=new ViewHolder1();
                viewHolder1.head= (TextView) convertView.findViewById(R.id.head);
                viewHolder1.img= (ImageView) convertView.findViewById(R.id.img);
                viewHolder1.update= (TextView) convertView.findViewById(R.id.update);
                viewHolder1.song1= (TextView) convertView.findViewById(R.id.song1);
                viewHolder1.song2= (TextView) convertView.findViewById(R.id.song2);
                viewHolder1.song3= (TextView) convertView.findViewById(R.id.song3);
                convertView.setTag(viewHolder1);
            }else {
                viewHolder1= (ViewHolder1) convertView.getTag();
            }
            MusicRank musicRank=list.get(position);
            if (position==0){
                viewHolder1.head.setVisibility(View.VISIBLE);
            }else {
                viewHolder1.head.setVisibility(View.GONE);
            }
            viewHolder1.img.setImageResource(musicRank.getImg());
            viewHolder1.update.setText(musicRank.getUpdate());
            viewHolder1.song1.setText(musicRank.getSong1());
            viewHolder1.song2.setText(musicRank.getSong2());
            viewHolder1.song3.setText(musicRank.getSong3());

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RankItem_fragment bang=new RankItem_fragment();
                    Bundle bundle=new Bundle();
                    bundle.putInt("wang",position);
                    bang.setArguments(bundle);
                    ((MainActivity)context).showFragment(bang);
                }
            });
            return convertView;
        }

        public View getView2(int position, View convertView, ViewGroup parent) {
            ViewHolder2 viewHolder1=null;
            if (convertView==null){
                convertView=LayoutInflater.from(context).inflate(R.layout.paihang_item2,null);
                viewHolder1=new ViewHolder2();
                viewHolder1.head= (TextView) convertView.findViewById(R.id.head);
                viewHolder1.img= (ImageView) convertView.findViewById(R.id.img);
                viewHolder1.update= (TextView) convertView.findViewById(R.id.update);
                viewHolder1.title= (TextView) convertView.findViewById(R.id.title);
                convertView.setTag(viewHolder1);
            }else {
                viewHolder1= (ViewHolder2) convertView.getTag();
            }
            MusicRank musicRank=list.get(position);
            if (position==4){
                viewHolder1.head.setVisibility(View.VISIBLE);
            }else {
                viewHolder1.head.setVisibility(View.GONE);
            }
            viewHolder1.img.setImageResource(musicRank.getImg());
            viewHolder1.update.setText(musicRank.getUpdate());
            viewHolder1.title.setText(musicRank.getTitle());

            return convertView;
        }
        class ViewHolder1{
            TextView head;
            ImageView img;
            TextView update;
            TextView song1;
            TextView song2;
            TextView song3;

        }
        class ViewHolder2{
            TextView head;
            ImageView img;
            TextView update;
            TextView title;
        }


    }
}
